﻿using MediatR;
using S.Produce.Api.Application.Commands.ShiftCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.ShiftCommandHandlers
{
    public class ShiftDeleteCommandHandler : IRequestHandler<ShiftDeleteCommand, int>
    {
        private readonly IRepository<ShiftDorm> shiftdorm;

        public ShiftDeleteCommandHandler(IRepository<ShiftDorm> shiftdorm)
        {
            this.shiftdorm = shiftdorm;
        }

        public async Task<int> Handle(ShiftDeleteCommand request, CancellationToken cancellationToken)
        {
            var ids = request.Ids.Split(",");
            var count = 0;
            var del = await shiftdorm.SelAllAsync(x => ids.Contains(x.SDId.ToString()));
            foreach (var item in del)
            {
                item.IsDel = true;
                count += await shiftdorm.UpdateAsync(item);
            }
            return count;
        }
    }
}
